﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using SqlAzureService.SalesSvc;
using System.Web;
using System.Data;


namespace SqlCustomerData.SalesPart
{
    public partial class SalesPartUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                string cacheName = "SqlAzureData" + this.ID;
                int cacheTime = 5;
                DataSet ds = null;

                //look for the data in cache
                if (Cache[cacheName] == null)
                {
                    //create an instance of the class
                    SqlAzureService.SqlData sql = new SqlAzureService.SqlData();

                    //set the properties so we can retrieve data
                    sql.WcfUrl = "https://azurewcf.vbtoys.com/Sales.svc";
                    sql.MethodName = "GetCustomerSales";
                    sql.OutputType = AzureConnect.WcfConfig.DataOutputType.ServerCache;
                    sql.ServerCacheName = cacheName;
                    sql.ServerCacheTime = cacheTime;

                    //execute the query to retrieve the data
                    bool success = sql.ExecuteRequest();

                    //if it worked then pull the data into our dataset
                    if (success)
                        ds = (DataSet)sql.QueryResultsObject;
                }
                else
                    ds = (DataSet)Cache[cacheName];

                //if we got data then bind it to the data grid
                if (ds != null)
                {
                    CustomerGrd.DataSource = ds;
                    CustomerGrd.DataBind();
                }
            }
            catch (Exception ex)
            {
                StatusLbl.Text = "There was an error retrieving data: " + ex.Message;
            }
        }
    }
}
